USE [master]
GO
ALTER DATABASE [wcfSyncSamplesDb] SET CHANGE_TRACKING = ON  (CHANGE_RETENTION = 2 DAYS)
GO


USE [wcfSyncSamplesDb]
GO
ALTER TABLE [Sales].[Customer] ENABLE CHANGE_TRACKING
GO


USE [wcfSyncSamplesDb]
GO
ALTER TABLE [Sales].[CustomerContact] ENABLE CHANGE_TRACKING 
GO

/**** Object:  StoredProcedure [dbo].[usp_GetNewBatchAnchor] ****/
CREATE PROCEDURE [dbo].[usp_GetNewBatchAnchor] (
	@sync_last_received_anchor bigint, 
	@sync_batch_size int,
	@sync_max_received_anchor bigint out,
	@sync_new_received_anchor bigint out,            
	@sync_batch_count int output)            
AS            
       -- Set a default batch size if a valid one is not passed in.
       IF  @sync_batch_size IS NULL OR @sync_batch_size <= 0
	     SET @sync_batch_size = 1000    

	   -- Before selecting the first batch of changes,
	   -- set the maximum anchor value for this synchronization           
         -- session. After the first time that this procedure is 
	   -- called, Synchronization Services passes a value for 
         -- @sync_max_received_anchor to the procedure. Batches of 
         -- changes are synchronized until this value is reached.
       IF @sync_max_received_anchor IS NULL
         SELECT  @sync_max_received_anchor = 
                    change_tracking_current_version()
       
       -- If this is the first synchronization session for a database,
       -- get the lowest change version value from the tables. By 
       -- default, Synchronization Services uses a value of 0 for 
       -- sync_last_received_anchor on the first synchronization. If        
       -- you do not set @sync_last_received_anchor,
       -- this can cause empty batches to be downloaded until the 
       -- lowest change version value is reached.
       IF @sync_last_received_anchor IS NULL OR 
              @sync_last_received_anchor = 0
       BEGIN
		SELECT @sync_last_received_anchor = 0

        -- Changes are only retained in the change table for a limited 
        -- period of time set by the CHANGE_RETENTION parameter 
        -- (on ALTER DATABASE).
        -- Check that we haven't had changes cleaned up on this table
        -- (i.e. CHANGE_TRACKING_MIN_VALID_VERSION returns > 0)        
		IF CHANGE_TRACKING_MIN_VALID_VERSION(
               object_id(N'dbo.Customers')) > 
                   @sync_last_received_anchor 
		  RAISERROR (N'SQL Server Change Tracking has cleaned up 
tracking information for table ''%s''. To recover from this error, the 
client must reinitialize its local database and try 
again',16,3,N'dbo.Customers')

		SET @sync_new_received_anchor = 
                @sync_last_received_anchor + @sync_batch_size

		-- Determine how many batches are required during the 
            -- initial synchronization.
		IF @sync_batch_count <= 0
		  SET @sync_batch_count = (
                 (@sync_max_received_anchor / @sync_batch_size) - 
                 (@sync_last_received_anchor /  @sync_batch_size)
              )
       END
     ELSE
       BEGIN
         SET @sync_new_received_anchor = 
           @sync_last_received_anchor + @sync_batch_size

         -- Determine how many batches are required during subsequent 
         -- synchronizations.
		IF @sync_batch_count <= 0
              SET @sync_batch_count = (
                 (@sync_max_received_anchor / @sync_batch_size) - 
                 (@sync_new_received_anchor /  @sync_batch_size)) + 1 
 
	 END

     -- Check whether this is the last batch.      
     IF @sync_new_received_anchor >= @sync_max_received_anchor
     BEGIN
       SET @sync_new_received_anchor = @sync_max_received_anchor        
       IF @sync_batch_count <= 0
         SET @sync_batch_count = 1
     END
GO
